package mn.buya;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity{

//	private DrawCanvas mycanvas;
	private DBAdapter db = new DBAdapter();
	private EditText etWord;
	private Context cThis;
	// Асуултын үг хаана яваа вэ гэдгийг тодорхойлно.
	int iCursorWord = -1;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
//		mycanvas = (DrawCanvas) findViewById(R.id.SurfaceView01);
//		mycanvas.startDrawImage();

		this.initail();
		this.loadData();

		WordTableGenerator wtgenerator = new WordTableGenerator(this);
		wtgenerator.generateWordTable(this);
		this.setListener();
	}

	private boolean loadData() {

		cThis = this;
		
		db.getRawWords();
		db.getAnswer();

		TextView tvQuestion = (TextView) findViewById(R.id.tvquestion);
		tvQuestion.setText(db.getQuestion());

		return true;
	}

	/**
	 * Programm эхэлхэд нэг удаа хийгдэх үйлдлүүд хийгдэнэ.
	 */
	private void initail() {

		Shared.DISPLAY_HEIGHT = getWindow().getWindowManager()
				.getDefaultDisplay().getHeight();
		Shared.DISPLAY_WIDTH = getWindow().getWindowManager()
				.getDefaultDisplay().getWidth();
		
		Shared.DIFFICULT = Shared.EASY;
		etWord = (EditText) findViewById(R.id.etword);
		etWord.setTextColor(Color.RED);
		TextView tvAnswer = (TextView) findViewById(R.id.tvanswer);
		tvAnswer.setTextColor(Color.RED);
		TextView tvQuestion = (TextView) findViewById(R.id.tvquestion);
		tvQuestion.setTextColor(Color.BLUE);

	}

	private void setListener() {

		etWord.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				check(etWord.getText().toString(), cThis);
			}
		});
	}
	
	public void check(String sValue, Context ctx) {
		System.out.println("Call Check : "+sValue);
//		if (!Shared.isAnim) {
			if (checkTrueWord(sValue)) {
//				animRun(etWord);
				System.out.println("Энэ үг мөн байна!");
				KeyAndValueTrue(ctx);
			}
//		}
	}
	
	
	String tmpAnswer = "";
	private void KeyAndValueTrue(Context ctx) {
		
		EditText etWord = (EditText) ((Activity) ctx).findViewById(R.id.etword);
		etWord.setText("");
		tmpAnswer = tmpAnswer + db.getAnswer().get(iCursorWord-1) + " ";
		TextView tvAnswer = (TextView) ((Activity) ctx).findViewById(R.id.tvanswer);
		tvAnswer.setText(tmpAnswer);
	}

	/**
	 * Anim эхлүүлнэ
	 * @param etWord
	 * @param sWord
	 */
//	private void animRun(final EditText etWord) {
//		
//		Shared.isAnim = true;
//		
//		final String sWord = db.getAnswer().get(iCursorWord-1);
//		
//		Shared.fAnimWordX = 7;
//		Shared.fAnimWordY = 115;
//		Shared.sAnimWord = sWord;
//		System.out.println("Animing Word : "+sWord);
//		etWord.setText("");
//		
//	}

	/**
	 * Үнэн үг эсхийг шалгадаг метод
	 */
	private boolean checkTrueWord(String sEtWord) {

		if (iCursorWord == -1) {
			iCursorWord = 0;
		}

		if (iCursorWord >= db.getAnswer().size()) {
			Step step = new Step();
			step.stepUp();
			return false;
		} else {
			String sCursorWord = db.getAnswer().get(iCursorWord);

			// Хариултын үгтэй тэнцүү эсхийг шалгаж байна.
			sEtWord = sEtWord.toLowerCase();
			sCursorWord = sCursorWord.toLowerCase();
			
			sEtWord = this.clearRawChar(sEtWord);
			if(Shared.DIFFICULT == Shared.EASY) {

				if(sEtWord.length() > 1 || sCursorWord.equals(sEtWord)) {
					if(sCursorWord.contains(sEtWord)) {
						iCursorWord++;
						return true;
					} else {
						return false;
					}
				}
			}
			
			if(Shared.DIFFICULT == Shared.MEDIUM) {
				if(sEtWord.length() > 3 || sCursorWord.equals(sEtWord)) {
					if(sCursorWord.contains(sEtWord)) {
						iCursorWord++;
						return true;
					} else {
						return false;
					}
				}
			}
			
			if(Shared.DIFFICULT == Shared.HARD) {
				if(sCursorWord.equals(sEtWord)) {
					if(sCursorWord.contains(sEtWord)) {
						iCursorWord++;
						return true;
					} else {
						return false;
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * Үгийн ард болон урд байх илүү дутуу тэмдэгтүүдийг устгаж цэвэрлэнэ.
	 * @param sWord
	 * @return
	 */
	private String clearRawChar(String sWord) {
		
		return sWord;
	}

}